<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 用户组管理
 */
class UserGroupController extends AdminController {

    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'menu' => array(
                    array(
                        'name' => '用户组列表',
                        'url' => url('index'),
                        'icon' => 'bars',
                    ),
                    array(
                        'name' => '添加用户组',
                        'url' => url('add'),
                        'icon' => 'plus',
                    ),
                ),
            );
    }

    /**
     * 列表
     */
    public function index(){
        //筛选条件
        $where = array();
        $where['special'] = 0;
        $where['admin'] = 0;
        $pageList = target('forum/UserGroup')->loadList($where);
        $where = array();
        $where[] = 'special = 1 or admin = 1';
        $specialList = target('forum/UserGroup')->loadList($where);
        //设置目录导航
        $this->adminCrumb[] = array('url'=>url('index'),'name'=>'用户组管理');
        $this->assign('pageList', $pageList);
        $this->assign('specialList', $specialList);
        $this->adminDisplay();
    }

    /**
     * 添加
     */
    public function add(){
        if(!IS_POST){
            //设置目录导航
            $this->adminCrumb[] = array('url'=>url('index'),'name'=>'用户组管理');
            $this->adminCrumb[] = array('url'=>url('edit',array('group_id'=>$info['group_id'])),'name'=>'用户编辑');
            $this->assign('info',$info);
            $this->assign('name','添加');
            $this->adminDisplay('info');
        }else{
            if(target('forum/UserGroup')->saveData('add')){
                $this->success('用户组添加成功！',url('index'));
            }else{
                $msg = target('forum/UserGroup')->getError();
                if(empty($msg)){
                    $this->error('用户组添加失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 编辑
     */
    public function edit(){
    	if(!IS_POST){
    		$groupId = request('get.group_id','','intval');
            if(empty($groupId)){
                $this->error('参数不能为空！');
            }
            $model = target('forum/UserGroup');
            $info = $model->getInfo($groupId);
            if(!$info){
                $this->error($model->getError());
            }
	    	//设置目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'用户组管理');
	    	$this->adminCrumb[] = array('url'=>url('edit',array('group_id'=>$info['group_id'])),'name'=>'用户组编辑');
	        $this->assign('name','编辑');
	        $this->assign('info',$info);
	        $this->adminDisplay('info');
        }else{
            if(target('forum/UserGroup')->saveData('edit')){
                $this->success('用户编辑成功！',url('index'));
            }else{
                $msg = target('forum/UserGroup')->getError();
                if(empty($msg)){
                    $this->error('用户编辑失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 删除
     */
    public function del(){
        $groupId = request('post.data');
        if(empty($groupId)){
            $this->error('参数不能为空！');
        }
        //删除操作
        if(target('forum/UserGroup')->delData($groupId)){
            $this->success('用户组删除成功！');
        }else{
            $msg = target('forum/UserGroup')->getError();
            if(empty($msg)){
                $this->error('用户组删除失败！');
            }else{
                $this->error($msg);
            }
        }
    }
}

